package com.sunshine.user.service.impl;

import com.sunshine.user.entity.UserAddresses;
import com.sunshine.user.mapper.UserAddressesDao;
import com.sunshine.user.service.UserAddressesService;
import com.sunshine.util.TokenUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import sunshine.commcon.util.entity.Result;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
@Slf4j
@Service
public class UserAddressesServiceImpl implements UserAddressesService {

    @Resource
    private UserAddressesDao userAddressesDao;

    @Override
    public Result<List<UserAddresses>> findUserAddressesByToken( UserAddresses userAddresses) {
        List<UserAddresses> userAddressesList = userAddressesDao.queryAllByLimit(userAddresses);
        log.info("查询用户收货地址成功: {}", userAddressesList);
        return Result.ok(userAddressesList);
    }

    @Override
    public Result<Boolean> addUserAddresses(UserAddresses userAddresses) {
        return userAddressesDao.insert(userAddresses) > 0 ? Result.ok(true) : Result.fail();
    }

    @Override
    public Result<Boolean> deleteAddressById(Long addressId, Long userId) {
        // 先验证地址是否属于该用户
        UserAddresses address = userAddressesDao.queryById(userId);
        if (address == null) {
            return Result.fail("地址不存在");
        }
        
        if (!address.getUserId().equals(userId)) {
            return Result.fail("无权限删除该地址");
        }
        Long addressId1 = address.getAddressId();
        log.info("删除用户地址，地址ID: {}，用户ID: {}", addressId1, userId);

        // 验证通过后执行删除
        return userAddressesDao.deleteById(addressId1) > 0 ? Result.ok(true) : Result.fail();
    }
}